#ifndef _SI4432_H_
#define _SI4432_H_

#include "bflb_mtimer.h"
#include "board.h"
#include "bflb_gpio.h"
#include "log.h"
#include <FreeRTOS.h>
#include <queue.h>
#include <timers.h>
#include <task.h>

/*设备id*/
#define RF_DEVICE_ID       0x00

/* 收发模式切换控制端*/
#define PAC_PIN            GPIO_PIN_27
/*  SDN*/
#define SDN_PIN            GPIO_PIN_30
/*  nIrq*/
#define IRQ_PIN            GPIO_PIN_31
/*  片选*/
#define SPI_CS_PIN         GPIO_PIN_14
/*  时钟*/
#define SPI_CLK_PIN        GPIO_PIN_13
/*  主机输入从机输出*/
#define SPI_MISO_PIN       GPIO_PIN_18
/*  从机输入主机输出*/
#define SPI_MOSI_PIN       GPIO_PIN_19


//-----------------------------------------------------RF4432配置口定义---------------------------------------------
#define	RF4432_PAC_1    bflb_gpio_set(gpio, PAC_PIN)		//收发模式切换控制端
#define	RF4432_PAC_0    bflb_gpio_reset(gpio, PAC_PIN)
//#define	RF4432_SDO_1    (P2OUT |= BIT3)		//SPI数据输出
//#define	RF4432_SDO_0    (P2OUT &= ~BIT3)
#define	RF4432_SDO_IN   bflb_gpio_read(gpio, SPI_MISO_PIN)
#define RF4432_SDI_1    bflb_gpio_set(gpio, SPI_MOSI_PIN)//SPI数据输入
#define RF4432_SDI_0    bflb_gpio_reset(gpio, SPI_MOSI_PIN)
#define RF4432_SCLK_1   bflb_gpio_set(gpio, SPI_CLK_PIN)//SPI时钟输入端口
#define RF4432_SCLK_0   bflb_gpio_reset(gpio, SPI_CLK_PIN)
#define RF4432_SEL_1    bflb_gpio_set(gpio, SPI_CS_PIN) //SPI片选
#define RF4432_SEL_0    bflb_gpio_reset(gpio, SPI_CS_PIN)
#define	RF4432_SDN_1    bflb_gpio_set(gpio, SDN_PIN)	        //SDN=1时关闭芯片工作	,SDN=0时芯片正常工作 
#define	RF4432_SDN_0    bflb_gpio_reset(gpio, SDN_PIN)
//#define	RF4432_IRQ_1    (P2OUT |= BIT0)	        //SI4432工作状态指示端口
//#define	RF4432_IRQ_0    (P2OUT &= ~BIT0)
#define RF4432_IRQ_IN   bflb_gpio_read(gpio, IRQ_PIN)
//------------------------------------------------------------------------------------------------------------------
//#define	RF4432_IO0_1    (P1OUT |= BIT5)		//IO-0可以不用
//#define	RF4432_IO0_0    (P1OUT &= ~BIT5)
//#define	RF4432_IO1_1    (P1OUT |= BIT6)         //IO-1可以不用
//#define	RF4432_IO1_0    (P1OUT &= ~BIT6)
//#define	RF4432_IO2_1    (P1OUT |= BIT4)         //IO-2可以不用
//#define	RF4432_IO2_0    (P1OUT &= ~BIT4)
//-------------------------------------------射频芯片相关定义--------------------------------
#define RF4432_TxRxBuf_Len 8 			   //定义RF4432数据包长度
static uint8_t RF4432_TxRxBuf[RF4432_TxRxBuf_Len]=
{
	0xAA,0x02,0x01,0x0,0x00,0x00,0x00,0xFF
};

static uint8_t RF4432_RxRxBuf[RF4432_TxRxBuf_Len] = { 0 };
//---------------------RF4432 配置寄存器地址--------------------//


#define DEVICE_TYPE 				    0x00        //器件类型码
#define DEVICE_VERSION 			            0x01        //版本号
#define DEVICE_STATUS 				    0x02	//器件状态
#define INTERRUPT_STATUS_1			    0x03        //中断状态1
#define INTERRUPT_STATUS_2			    0x04        //中断状态2
#define INTERRUPT_ENABLE_1 			    0x05        //中断使能1
#define INTERRUPT_ENABLE_2 			    0x06        //中断使能2
#define OPERATING_FUNCTION_CONTROL_1		    0x07	//工作模式和功能控制1
#define OPERATING_FUNCTION_CONTROL_2 		    0x08	//工作模式和功能控制2
#define CRYSTAL_OSCILLATOR_LOAD_CAPACITANCE	    0x09	//晶振负载电容设置
#define MICROCONTROLLER_OUTPUT_CLOCK 		    0x0A        //微控制器输出时钟
#define GPIO0_CONFIGURATION 			    0x0B	//GPIO0功能设置寄存器见英文文档第105页
#define GPIO1_CONFIGURATION 			    0x0C
#define GPIO2_CONFIGURATION 			    0x0D
#define IO_PORT_CONFIGURATION 			    0x0E        //IO端口配置       
#define ADC_CONFIGURATION			    0x0F        //ADC配置
#define ADC_SENSOR_AMPLIFIER_OFFSET		    0x10        //ADC传感放大器偏差
#define ADC_VALUE 				    0x11        //ADC值
#define TEMPERATURE_SENSOR_CONTROL		    0x12	//温度传感器校准
#define TEMPERATURE_VALUE_OFFSET 		    0x13        //温度传感器值偏差
#define WAKE_UP_TIMER_PERIOD_1			    0x14        //唤醒定时器时期1
#define WAKE_UP_TIMER_PERIOD_2			    0x15        //唤醒定时器时期2
#define WAKE_UP_TIMER_PERIOD_3			    0x16        //唤醒定时器时期3
#define WAKE_UP_TIMER_VALUE_1 			    0x17        //唤醒定时器值1
#define WAKE_UP_TIMER_VALUE_2 			    0x18        //唤醒定时器值2
#define LOW_DUTY_CYCLE_MODE_DURATION 		    0x19	//低任务周期模式持续时间
#define LOW_BATTERY_DETECTOR_THRESHOLD		    0x1A	//低压检测阈值寄存器
#define BATTERY_VOLTAGE_LEVEL			    0x1B        //电池电压级别
#define IF_FILTER_BANDWIDTH 			    0x1C	//中频滤波器（IF）带宽寄存器
#define AFC_LOOP_GEARSHIFT_OVERRIDE 		    0x1D        //AFC循环变速超速
#define AFC_TIMING_CONTROL			    0x1E        //AFC定时控制
#define CLOCK_RECOVERY_GEARSHIFT_OVERRIDE 	    0x1F        //时钟恢复变速超速
#define CLOCK_RECOVERY_OVERSAMPLING_RATIO 	    0x20        //时钟恢复过抽样率
#define CLOCK_RECOVERY_OFFSET_2 		    0x21        //时钟恢复偏差
#define CLOCK_RECOVERY_OFFSET_1			    0x22
#define CLOCK_RECOVERY_OFFSET_0 		    0x23
#define CLOCK_RECOVERY_TIMING_LOOP_GAIN_1	    0x24        //时钟恢复定时循环增益
#define CLOCK_RECOVERY_TIMING_LOOP_GAIN_0 	    0x25
#define RECEIVED_SIGNAL_STRENGTH_INDICATOR 	    0x26        //接收信号强度指示器（RSSI）
#define RSSI_THRESHOLD_FOR_CLEAR_CHANNEL_INDICATOR  0x27        //无干扰信道指示器接收信号强度指示（RSSI）
#define ANTENNA_DIVERSITY_REGISTER_1 		    0x28        //天线分集
#define ANTENNA_DIVERSITY_REGISTER_2 		    0x29
#define DATA_ACCESS_CONTROL 			    0x30        //时间存储控制
#define EZMAC_STATUS 				    0x31        //EzMAC状态
#define HEADER_CONTROL_1 			    0x32	//Header 起始码设置
#define HEADER_CONTROL_2 			    0x33
#define PREAMBLE_LENGTH 			    0x34	//前导码长度
#define PREAMBLE_DETECTION_CONTROL		    0x35	//前导码检测设置
#define SYNC_WORD_3 				    0x36	//同步字节
#define SYNC_WORD_2 				    0x37
#define SYNC_WORD_1				    0x38
#define SYNC_WORD_0 				    0x39
#define TRANSMIT_HEADER_3 			    0x3A        //发射帧头
#define TRANSMIT_HEADER_2			    0x3B
#define TRANSMIT_HEADER_1			    0x3C
#define TRANSMIT_HEADER_0			    0x3D
#define TRANSMIT_PACKET_LENGTH			    0x3E	//发送数据包长度，详细请看P125页
#define CHECK_HEADER_3				    0x3F        //检测帧头
#define CHECK_HEADER_2				    0x40
#define CHECK_HEADER_1 				    0x41
#define CHECK_HEADER_0				    0x42
#define HEADER_ENABLE_3                             0x43        //帧头使能
#define HEADER_ENABLE_2                             0x44
#define HEADER_ENABLE_1                             0x45
#define HEADER_ENABLE_0                             0x46
#define RECEIVED_HEADER_3                           0x47        //接收到的帧头
#define RECEIVED_HEADER_2                           0x48
#define RECEIVED_HEADER_1                           0x49
#define RECEIVED_HEADER_0                           0x4A
#define RECEIVED_PACKET_LENGTH                      0x4B	//接收数据包长度，详细请看P134页
#define ANALOG_TEST_BUS                             0x50        //模拟测试总线选择
#define DIGITAL_TEST_BUS_ENSCTEST_                  0x51        //数据测试总线选择
#define TX_RAMP_CONTROL                             0x52        //发射斜率控制
#define PLL_TUNE_TIME                               0x53	//锁相环（PPL）切换时间
#define CALIBRATION_CONTROL                         0x55        //校正控制
#define MODEM_TEST                                  0x56        //调制控制
#define CHARGEPUMP_TEST                             0x57        //充电泵测试
#define CHARGEPUMP_CURRENT_TRIMMING_OVERRIDE        0x58        //充电泵电流微调/超调
#define DIVIDER_CURRENT_TRIMMING             	    0x59        //电流分压器微调/Delta-Sigma测试
#define VCO_CURRENT_TRIMMING               	    0x5A        //VCO电流微调
#define VCO_CALIBRATION_OVERRIDE     		    0x5B        //VCO校正/超驰
#define SYNTHESIZER_TEST            		    0x5C        //合成器测试
#define BLOCK_ENABLE_OVERRIDE_1			    0x5D        //模块使能超驰
#define BLOCK_ENABLE_OVERRIDE_2			    0x5E
#define BLOCK_ENABLE_OVERRIDE_3			    0x5F
#define CHANNEL_FILTER_COEFFICIENT_ADDRESS	    0x60        //频道滤波器系数地址
#define CHANNEL_FILTER_COEFFICIENT_VALUE	    0x61        //频道滤波器系数值
#define CRYSTAL_OSCILLATOR_CONTROL_TEST	     	    0x62        //晶体振荡器/上电复位控制
#define RC_OSCILLATOR_COARSE_CALIBRATION_OVERRIDE   0x63        //RC振荡器粗略校正/超驰
#define RC_OSCILLATOR_FINE_CALIBRATION_OVERRIDE	    0x64        //RC震荡器精细校正
#define LDO_CONTROL_OVERRIDE_ENSPOR	     	    0x65        //LDO控制超驰
#define LDO_LEVEL_SETTING	     	   	    0x66        //保留？LDO等级设置？
#define DELTASIGMA_ADC_TUNING_1	     	    	    0x67        //Delta-sigma ADC调谐
#define DELTASIGMA_ADC_TUNING_2	     	   	    0x68
#define AGC_OVERRIDE_1	     	     	            0x69        //AGC过载
#define AGC_OVERRIDE_2	     	   		    0x6A
#define GFSK_FIR_FILTER_COEFFICIENT_ADDRESS	    0x6B        //GFSK FIR 滤波器系数地址
#define GFSK_FIR_FILTER_COEFFICIENT_VALUE	    0x6C        //GFSK FIR 滤波器系数值
#define TX_POWER	     	  		    0x6D        //发射功率设置,详细请见P153
#define TX_DATA_RATE_1	     			    0x6E	//数据发送波特率设置寄存器1
#define TX_DATA_RATE_0	     	 		    0x6F	//数据发送波特率设置寄存器0
#define MODULATION_MODE_CONTROL_1  	 	    0x70	//调制方式控制,详细请见P155
#define MODULATION_MODE_CONTROL_2	 	    0x71
#define FREQUENCY_DEVIATION	 		    0x72        //频率偏差
#define FREQUENCY_OFFSET_1	 		    0x73        //频率偏移
#define FREQUENCY_OFFSET_2	 		    0x74        //频道控制
#define FREQUENCY_BAND_SELECT	 		    0x75	//频段选择,详细请见P157
#define NOMINAL_CARRIER_FREQUENCY_1	 	    0x76	//基准载波频率
#define NOMINAL_CARRIER_FREQUENCY_0	 	    0x77
#define FREQUENCY_HOPPING_CHANNEL_SELECT	    0x79	//跳频频道选择,详细请见P158
#define FREQUENCY_HOPPING_STEP_SIZE	 	    0x7A	//跳频频道间隔
#define TX_FIFO_CONTROL_1	 		    0x7C
#define TX_FIFO_CONTROL_2	 		    0x7D
#define RX_FIFO_CONTROL	 			    0x7E
#define FIFO_ACCESS	 			    0x7F        //FIFO读写方式设置


//-----------------------------RF4432控制指令---------------------------//
#define RR		0x00
#define WR		0x80


//------------------------------函数申明-----------------------------------//
void si4432_task(void* arg);
uint8_t SPI_Read(void);					        //SPI单字节读取函数
void SPI_Write(uint8_t txdata);					//SPI单字节写入函数
uint8_t  RF4432_ReadReg(uint8_t  addr);		                //RF4432寄存器读取函数
void  RF4432_WriteReg(uint8_t  addr, uint8_t value);	        //RF4432寄存器写函数
void  RF4432_WriteBurestReg(uint8_t  addr,uint8_t  *p,uint8_t  count);
void RF4432_ReadBurestReg(uint8_t  addr,uint8_t  *p,uint8_t  count);
void RF4432_Init(void);					        //RF4432射频芯片初始化函数
void RF4432_ReInit(void);					        //RF4432射频芯片初始化函数
void RF4432_SetRxMode(void);			                //RF4432设置接收模式函数
uint8_t RF4432_RxPacket(void);					//RF4432数据包接收函数
void RF4432_TxPacket(void);					//RF4432数据包发送函数
void RF4432_TxPacket_Send(uint8_t *data, uint16_t size);	//RF4432数据包发送函数
uint8_t get_ver(void);

//----------------------------测试用例--------------------------------------//
/*
发送端
void main()
{
  RF4432_Init();
  while(1)
  {
    RF4432_TxPacket();
  }
}
接收端
void main()
{
  RF4432_Init();
  RF4432_SetRxMode();
  while(1)
  {
    if(RF4432_RxPacket()) {
    }
  }
}
*/


#endif // !_SI4432__